package mystringbuilder;

import java.util.Arrays;

/**
 * @author small瑞
 * 能够存放数据（八种基本数据类型、String、StringBuffer、StringBuilder、Object）
 * 使用数组以及循环、判断、面向对象进行操作。（禁止使用集合、Stream等）
 * 能够添加数组（八种基本数据类型的数组以及引用类型的数据【String】）
 * 实现以下方法：
 * capacity()
 * ensureCapacity(int minimumCapacity)
 * length() ： 字符序列的长度
 * delete(int sta0t, int end) ： 从 start 到 end 都删掉 , 不包含end
 * indexOf(String str) ： 指定字符串第一次出现的位置
 * lastIndexOf(String str)
 * reverse() : 反转
 * replace(int start, int end, String str) : 从 start 到 end 全部替换成 str
 * substring(int start, int end)
 * toString() ： 变成字符串 ， 一定会 new String
 * append ， 构成重载
 */
public class MyStringBuilder {
    private int capacity ;
    private int count;

    public char[] getContent() {
        return content;
    }

    private char[] content ;

    public MyStringBuilder(){
        this.capacity = 16 ;
        this.count = 0 ;
    }
    public MyStringBuilder(int i){  //整形
        this.capacity = 16;
        String temp = Integer.toString(i) ;
        char content[] = temp.toCharArray();
        this.content = content;
        this.count = temp.length();
        if(this.count>this.capacity){
            this.capacity = temp.length();
        }
    }
    public MyStringBuilder(String i){  //整形
        this.capacity = 16;
        char content[] = i.toCharArray();
        this.content = content;
        this.count = i.length();
        if(this.count>this.capacity){
            this.capacity = i.length();
        }
    }
    public MyStringBuilder(byte i){  //整形
        this.capacity = 16;
        String temp = Byte.toString(i);
        char content[] = temp.toCharArray();
        this.content = content;
        this.count = temp.length();
        if(this.count>this.capacity){
            this.capacity = temp.length();
        }
    }
    public MyStringBuilder(boolean i){  //整形
        this.capacity = 16;
        String temp = Boolean.toString(i);
        char content[] = temp.toCharArray();
        this.content = content;
        this.count = temp.length();
        if(this.count>this.capacity){
            this.capacity = temp.length();
        }
    }
    public MyStringBuilder(short i){  //整形
        this.capacity = 16;
        String temp = Short.toString(i);
        char content[] = temp.toCharArray();
        this.content = content;
        this.count = temp.length();
        if(this.count>this.capacity){
            this.capacity = temp.length();
        }
    }
    public MyStringBuilder(long i){  //整形
        this.capacity = 16;
        String temp = Long.toString(i);
        char content[] = temp.toCharArray();
        this.content = content;
        this.count = temp.length();
        if(this.count>this.capacity){
            this.capacity = temp.length();
        }
    }
    public MyStringBuilder(float i){  //整形
        this.capacity = 16;
        String temp = Float.toString(i);
        char content[] = temp.toCharArray();
        this.content = content;
        this.count = temp.length();
        if(this.count>this.capacity){
            this.capacity = temp.length();
        }
    }
    public MyStringBuilder(double i){  //整形
        this.capacity = 16;
        String temp = Double.toString(i);
        char content[] = temp.toCharArray();
        this.content = content;
        this.count = temp.length();
        if(this.count>this.capacity){
            this.capacity = temp.length();
        }
    }
    public MyStringBuilder(Object i){  //整形
        this.capacity = 16;
        String temp = (String) i;
        char content[] = temp.toCharArray();
        this.content = content;
        this.count = temp.length();
        if(this.count>this.capacity){
            this.capacity = temp.length();
        }
    }
    public MyStringBuilder(StringBuilder i){  //整形
        this.capacity = 16;
        String temp = i.toString();
        char content[] = temp.toCharArray();
        this.content = content;
        this.count = temp.length();
        if(this.count>this.capacity){
            this.capacity = temp.length();
        }
    }
    public MyStringBuilder(StringBuffer i){  //整形
        this.capacity = 16;
        String temp = i.toString();
        char content[] = temp.toCharArray();
        this.content = content;
        this.count = temp.length();
        if(this.count>this.capacity){
            this.capacity = temp.length();
        }
    }
    public MyStringBuilder append(int i){
        String temp = Integer.toString(i);
        char content1[] = new char[this.count+temp.length()];
        //content1=Arrays.copyOf(this.content,this.length());
        for(int k = 0 ; k<this.count ;k++){
            content1[k] = this.content[k];
        }
        for(int k = this.count,l = 0;k < content1.length ; k++,l++){
            content1[k] = temp.charAt(l);
        }
        this.content = content1;
        this.count = content1.length;
        if(this.capacity<this.count){
            this.capacity = this.count;
        }
        return this;
    }
    public MyStringBuilder append(String i){
        char content1[] = new char[this.count+ i.length()];
        //content1=Arrays.copyOf(this.content,this.length());
        for(int k = 0 ; k<this.count ;k++){
            content1[k] = this.content[k];
        }
        for(int k = this.count,l = 0;k < content1.length ; k++,l++){
            content1[k] = i.charAt(l);
        }
        this.content = content1;
        this.count = content1.length;
        if(this.capacity<this.count){
            this.capacity = this.count;
        }
        return this;
    }
    public MyStringBuilder append(byte i){
        String temp = Byte.toString(i);
        char content1[] = new char[this.count+temp.length()];
        //content1=Arrays.copyOf(this.content,this.length());
        for(int k = 0 ; k<this.count ;k++){
            content1[k] = this.content[k];
        }
        for(int k = this.count,l = 0;k < content1.length ; k++,l++){
            content1[k] = temp.charAt(l);
        }
        this.content = content1;
        this.count = content1.length;
        if(this.capacity<this.count){
            this.capacity = this.count;
        }
        return this;
    }
    public MyStringBuilder append(short i){
        String temp = Short.toString(i);
        char content1[] = new char[this.count+temp.length()];
        //content1=Arrays.copyOf(this.content,this.length());
        for(int k = 0 ; k<this.count ;k++){
            content1[k] = this.content[k];
        }
        for(int k = this.count,l = 0;k < content1.length ; k++,l++){
            content1[k] = temp.charAt(l);
        }
        this.content = content1;
        this.count = content1.length;
        if(this.capacity<this.count){
            this.capacity = this.count;
        }
        return this;
    }
    public MyStringBuilder append(long i){
        String temp = Long.toString(i);
        char content1[] = new char[this.count+temp.length()];
        //content1=Arrays.copyOf(this.content,this.length());
        for(int k = 0 ; k<this.count ;k++){
            content1[k] = this.content[k];
        }
        for(int k = this.count,l = 0;k < content1.length ; k++,l++){
            content1[k] = temp.charAt(l);
        }
        this.content = content1;
        this.count = content1.length;
        if(this.capacity<this.count){
            this.capacity = this.count;
        }
        return this;
    }
    public MyStringBuilder append(float i){
        String temp = Float.toString(i);
        char content1[] = new char[this.count+temp.length()];
        //content1=Arrays.copyOf(this.content,this.length());
        for(int k = 0 ; k<this.count ;k++){
            content1[k] = this.content[k];
        }
        for(int k = this.count,l = 0;k < content1.length ; k++,l++){
            content1[k] = temp.charAt(l);
        }
        this.content = content1;
        this.count = content1.length;
        if(this.capacity<this.count){
            this.capacity = this.count;
        }
        return this;
    }
    public MyStringBuilder append(double i){
        String temp = Double.toString(i);
        char content1[] = new char[this.count+temp.length()];
        //content1=Arrays.copyOf(this.content,this.length());
        for(int k = 0 ; k<this.count ;k++){
            content1[k] = this.content[k];
        }
        for(int k = this.count,l = 0;k < content1.length ; k++,l++){
            content1[k] = temp.charAt(l);
        }
        this.content = content1;
        this.count = content1.length;
        if(this.capacity<this.count){
            this.capacity = this.count;
        }
        return this;
    }
    public MyStringBuilder append(char i){
        char content1[] = new char[this.count+1];
        //content1=Arrays.copyOf(this.content,this.length());
        for(int k = 0 ; k<this.count ;k++){
            content1[k] = this.content[k];
        }
        content1[this.count] = i;
        this.content = content1;
        this.count = content1.length;
        if(this.capacity<this.count){
            this.capacity = this.count;
        }
        return this;
    }
    public MyStringBuilder append(boolean i){
        String temp = Boolean.toString(i);
        char content1[] = new char[this.count+temp.length()];
        //content1=Arrays.copyOf(this.content,this.length());
        for(int k = 0 ; k<this.count ;k++){
            content1[k] = this.content[k];
        }
        for(int k = this.count,l = 0;k < content1.length ; k++,l++){
            content1[k] = temp.charAt(l);
        }
        this.content = content1;
        this.count = content1.length;
        if(this.capacity<this.count){
            this.capacity = this.count;
        }
        return this;
    }
    public MyStringBuilder append(int[] arr){
        for(int k = 0 ,len = arr.length; k< arr.length;k++){
            this.append(arr[k]);
        }
        return this;
    }
    public MyStringBuilder append(byte[] arr){
        for(int k = 0 ,len = arr.length; k< arr.length;k++){
            this.append(arr[k]);
        }
        return this;
    }
    public MyStringBuilder append(short[] arr){
        for(int k = 0 ,len = arr.length; k< arr.length;k++){
            this.append(arr[k]);
        }
        return this;
    }
    public MyStringBuilder append(long[] arr){
        for(int k = 0 ,len = arr.length; k< arr.length;k++){
            this.append(arr[k]);
        }
        return this;
    }
    public MyStringBuilder append(float[] arr){
        for(int k = 0 ,len = arr.length; k< arr.length;k++){
            this.append(arr[k]);
        }
        return this;
    }
    public MyStringBuilder append(double[] arr){
        for(int k = 0 ,len = arr.length; k< arr.length;k++){
            this.append(arr[k]);
        }
        return this;
    }
    public MyStringBuilder append(boolean[] arr){
        for(int k = 0 ,len = arr.length; k< arr.length;k++){
            this.append(arr[k]);
        }
        return this;
    }
    public MyStringBuilder append(String[] arr){
        for(int k = 0 ,len = arr.length; k< arr.length;k++){
            this.append(arr[k]);
        }
        return this;
    }
    public MyStringBuilder append(char[] arr){
        for(int k = 0 ,len = arr.length; k< len;k++){
            this.append(arr[k]);
        }
        return this;
    }
    public MyStringBuilder delete(int start, int end){
        char[] startarr = Arrays.copyOfRange(this.content,0,start);
        char[] endarr = Arrays.copyOfRange(this.content,end,this.count);
        char[] content1 = new char[this.count-end+start];
        this.content = content1 ;
        System.arraycopy(startarr,0,content1,0,startarr.length);
        System.arraycopy(endarr,0,content1,startarr.length,endarr.length);
        this.count = this.content.length;
        if(this.capacity < this.count){
            this.capacity = this.count;
        }
        return this;
    }
    public MyStringBuilder substring(int start , int end){
        char[] arr = Arrays.copyOfRange(this.content,start,end);
        this.content = arr;
        this.count = this.content.length;
        if(this.capacity < this.count){
            this.capacity = this.count;
        }
        return this;
    }
    public int indexOf(String str){
        String arr = new String(this.content);
        return arr.indexOf(str);
    }
    public int lastindexOf(String str){
        String arr = new String(this.content);
        return arr.lastIndexOf(str);
    }
    public MyStringBuilder reverse(){
        char arr[] = new char[this.count];
        for(int i = 0 ,n=this.count ; i <n; i++){
            arr[i] = this.content[n-i-1];
        }
        this.content = arr;
        return this;
    }
    public MyStringBuilder replace(int start, int end, String str){
        char[] startarr = Arrays.copyOfRange(this.content,0,start);
        char[] endarr = Arrays.copyOfRange(this.content,end,this.count);
        char[] cen = str.toCharArray();
        char[] arr = new char[startarr.length + endarr.length + cen.length];
        System.arraycopy(startarr,0,arr,0,startarr.length);
        System.arraycopy(cen,0,arr,startarr.length,cen.length);
        System.arraycopy(endarr,0,arr,cen.length+startarr.length,endarr.length);
        this.content = arr;
        this.count = this.content.length;
        if(this.capacity < this.count){
            this.capacity = this.count;
        }
        return this;
    }





    @Override
    public String toString() {
        return new String(content);
    }
    public int length(){
        return this.count;
    }
    public int Capacity(){ return this.capacity;}
    public MyStringBuilder ensureCapacity(int i){
        this.capacity = i;
        return this;
    }


}
